﻿var objRequestTutor=null;
//course标记 0获取course列表
var courseFlag=0;
var ArrCourse;
function IMchange(e)
{
   $('SIMAccount').value=IMAccountList[0][e.value-1]; 
   $('TIMAccount').innerHTML=IMAccountList[1][e.value-1]; 
}
function LongTimeChange(e)
{
    $("TolPrice").innerHTML=Math.round(e.value/2*price);
}
//改变lesson
function LessonChange(e)
{
    onLongTimeText(e.value.split(":")[1]);
}
function onLongTimeText(longTime)
{
    //price=lprice;
    var strAppend = new Sys.StringBuilder(); 
    strAppend.append("<select id='selLongTime' onchange='LongTimeChange(this)'>");   
    for (var i = 1; i <= longTime; i++)
    {
        var tmp="<option value='"+i+"' "+(i==longTime?"selected='selected'":"")+">"+hourText.replace("{0}",i / 2)+"</option>";
        strAppend.append(tmp);
    }
    strAppend.append("</select>");
    var sTemp = strAppend.toString();
    $("LBselLongTime").innerHTML=sTemp;
    //$("PriceDiv").innerHTML=price;
    $("TolPrice").innerHTML=Math.round(longTime/2*price);
}
function GetRequestTutorInfo()
{
    if(objRequestTutor==null)
        WebServices.User.Teach.GetRequestTutorInfo(TNO,onsuccRequestTutor);
    else
        onsuccRequestTutor(objRequestTutor);
}
function onsuccRequestTutor(result)
{
    selCID=-1;
    objRequestTutor=result;
    if(result!=null)
    {
        $("LessonID").innerHTML="";
        $("TeachTitle").innerHTML=TP250;  
        //
        var strAppend = new Sys.StringBuilder();
        strAppend.append("<select id='selTL'>");
        var tmp="<option value='{0}'>{1}</option>";        
        if (result.TLang1 != "")
            strAppend.append(tmp.replace("{0}",result.TLang1).replace("{1}",GetLanguageLabel("selLang",result.TLang1)));
        if (result.TLang2 != "")
            strAppend.append(tmp.replace("{0}",result.TLang2).replace("{1}",GetLanguageLabel("selLang",result.TLang2)));
        if (result.TLang3 != "")
            strAppend.append(tmp.replace("{0}",result.TLang3).replace("{1}",GetLanguageLabel("selLang",result.TLang3)));
        if (result.TLang4 != "")
            strAppend.append(tmp.replace("{0}",result.TLang4).replace("{1}",GetLanguageLabel("selLang",result.TLang4)));
        strAppend.append("</select>");
        $("LBselTL").innerHTML=strAppend.toString();
        //
        strAppend = new Sys.StringBuilder();
        strAppend.append("<select id='selBL'>");        
        if (result.MotherTongue != "")
            strAppend.append(tmp.replace("{0}",result.MotherTongue).replace("{1}",GetLanguageLabel("selLang",result.MotherTongue)));
        if (result.MotherTongue2 != "")
            strAppend.append(tmp.replace("{0}",result.MotherTongue2).replace("{1}",GetLanguageLabel("selLang",result.MotherTongue2)));
        if (result.MotherTongue3 != "")
            strAppend.append(tmp.replace("{0}",result.MotherTongue3).replace("{1}",GetLanguageLabel("selLang",result.MotherTongue3)));
        if (result.MotherTongue4 != "")
            strAppend.append(tmp.replace("{0}",result.MotherTongue4).replace("{1}",GetLanguageLabel("selLang",result.MotherTongue4)));
        strAppend.append("</select>");
        $("LBselBL").innerHTML=strAppend.toString();
        //
        price=result.Price;
        $("PriceDiv").innerHTML=price;
        onLongTimeText(4);        
    }
    courseFlag=0;
}
function GetCourses()
{
    if(courseFlag==0)
        WebServices.User.Teach.GetUserCourses(TNO,onsuccCourse);
}
function onsuccCourse(result)
{
    if(result!=null && result.length>0)
    {
        var domDiv =$("BookCourseID");        
        if(domDiv!=null)
        {    //        
            var listBuilder = new Sys.StringBuilder();
            listBuilder.append("<select id='selCourse' onchange='GetACourseLessons(this)'>");
            var len=result[0].length;
            ArrCourse=result[0];            
            for(var i=0;i<len;i++)
            {
                var obj = result[0][i];
                listBuilder.append("<option value='"+obj.CID+"'>"+obj.Title+"</option>"); 
            }
            listBuilder.append("</select>")
            var sTemp = listBuilder.toString();
            selCID=ArrCourse[0].CID;
            domDiv.innerHTML = sTemp;
            price=ArrCourse[0].Price;
            $("PriceDiv").innerHTML=price;
            onsucc(result[1],0);
            courseFlag=1;
        }
    }
}
function GetACourseLessons(e)
{
    selCID=e.value;
    WebServices.User.Teach.GetACourseLessons(TNO,selCID,onsuccLesson);
}
function onsuccLesson(result)
{
    if(result!=null&&result.length>0)
    {
        var cLen=ArrCourse.length;
        var cIndex=0;
        for(var i=0; i<cLen; i++)
        {
            if(selCID==ArrCourse[i].CID)
                cIndex=i;
        }
        onsucc(result,cIndex);
    }
}
function onsucc(objValue,index)
{
    $("LBselTL").innerHTML="<select id='selTL'><option value='"+ArrCourse[index].LanguageLearn+"'>"+GetLanguageLabel("selLang",ArrCourse[index].LanguageLearn)+"</option></select>";
    var arrTL=ArrCourse[index].BaseLanguage.split("|");
    var listBuilder = new Sys.StringBuilder();
    listBuilder.append("<select id='selBL'>");
    for(var i=0;i<arrTL.length;i++)
    {
        var tmp= "<option value='"+arrTL[i]+"'>"+GetLanguageLabel("selLang",arrTL[i])+"</option>";
        listBuilder.append(tmp);
    }
    listBuilder.append("</select>");
    $("LBselBL").innerHTML=listBuilder.toString();
    //
    $("TeachTitle").innerHTML=ArrCourse[index].Title;             
    //
    var domDivLesson = $("LessonID");
    var ilongTime=0;
    ilongTime=objValue[0].LongTime;
    if(domDivLesson!=null)
    {
        listBuilder = new Sys.StringBuilder();
        listBuilder.append("<div class='item'>");
        listBuilder.append(WLText);
        listBuilder.append("</div><div <div class='op'><select id='selLesson' onchange='LessonChange(this)'>");
        var lLen=objValue.length;
        for(var i=0;i<lLen;i++)
        {
            var lobj = objValue[i];                
            var tmp="<option value='"+lobj.LID+":"+lobj.LongTime+"'>"+LTText.replace("{0}",lobj.Chapter.toString()).replace("{1}",lobj.Title)+"</option>";
            listBuilder.append(tmp); 
        }
        listBuilder.append("</select></div><div class='clear'></div>")
        var slTemp = listBuilder.toString();
        //alert(slTemp);
        domDivLesson.innerHTML = slTemp;
    }
    //
    price=ArrCourse[index].Price;
    $("PriceDiv").innerHTML=price;        
    onLongTimeText(ilongTime);    
}
function SubSave()
{
    if(!$ValidButton$())return;
    $('spAgree').style.display="none";
    $("spStartTime").style.display="none";
    $("spMsg").style.display="none";    
    var txtMsg = $('txtCommInput').value.replace(/<script[^>]*>([\u0001-\uFFFF]*?)<\/script>/gi,"");
    if(txtMsg.length>3700) {$("spMsg").style.display="";return;}
    if(!$('agree').checked){$('spAgree').style.display="";return;}
    showProgressBar(true);
    $("btSave").disabled=true;
    var lid=-1;
    if(selCID>0)
    {
        lid=$('selLesson').value.split(":")[0];
    }
    WebServices.User.Teach.AddSession(selCID, lid, TNO, $('selTL').value, $('selBL').value, $('selDayTime').value, $('selHour').value,$('selMinutes').value, $('selLongTime').value,price, $('selIM').value, $('selAudioType').value,$('SIMAccount').value,txtMsg,timezone,onsuccSub);    
}
function onsuccSub(result)
{
    showProgressBar(false);
    $("btSave").disabled=false;
    if(result!=null)
    {
        if(result[0].toString()=="0")
        {
            $("MsgSession").style.display="";
            if(selCID>0) $("MsgPrompt").innerHTML=MsgFail2;
            else $("MsgPrompt").innerHTML=MsgFail1;
        }
        else if(result[0].toString()=="TimeError")
        {
            $("spStartTime").style.display="";
        }
        else
        { 
            top.location='/session/session.htm?sid='+result[1].SessionID;
        }
    }else alert("error");
}
